package com.lakeworks.web.controller.ticket;


import com.lakeworks.common.core.controller.BaseController;
import com.lakeworks.common.core.domain.AjaxResult;
import com.lakeworks.common.domain.ticket.TktClaims;
import com.lakeworks.common.domain.ticket.TktTicket;
import com.lakeworks.domain.vo.TicketBackVo;
import com.lakeworks.event.NotreachedEvent;
import com.lakeworks.service.ITktClaimsService;
import com.lakeworks.service.ITktTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 工单Controller
 *
 * @author lake
 * @date 2023-07-17
 */
@RestController
@RequestMapping("/tkt")
@Validated
public class TktTicketController extends BaseController {

    @Autowired
    private ITktTicketService tktTicketService;

    @Autowired
    private ITktClaimsService claimsService;

    /**
     * 获取工单列表
     */
    @GetMapping("/tickets")
    public AjaxResult list(TktTicket tktTicket) {
        return AjaxResult.success(tktTicketService.selectTktTicketList(tktTicket));
    }

    /**
     * 添加工单
     */
    @PostMapping("/ticket")
    public AjaxResult add(@Valid @RequestBody TktTicket tktTicket) {
        return tktTicketService.addTktTicket(tktTicket);
    }

    /**
     * 工单详情
     */
    @GetMapping("/ticket/detail")
    public AjaxResult detail(@NotNull(message = "工单id不能为空") Long ticketId) {
        return tktTicketService.ticketDatil(ticketId);
    }

    /**
     * 根据节点获取ce选项
     */
    @GetMapping("/ticket/options")
    public AjaxResult getOptions(@NotNull(message = "节点id不能为空") Long nodeId, @NotNull(message = "工单id不能为空") Long ticketId) {
        return tktTicketService.getOptions(nodeId, ticketId);
    }


    /**
     * 更新工单
     */
    @PatchMapping("/ticket")
    public AjaxResult updateTicket(@RequestBody TicketBackVo ticketBackVo) {
        return tktTicketService.updateTicket(ticketBackVo);
    }

    @PostMapping("/notreached")
    public AjaxResult notreached(@RequestBody NotreachedEvent event){
        return tktTicketService.notreached(event);
    }


    /**
     * 执行决定
     */
    @PostMapping("/option")
    public AjaxResult backTicket(@RequestBody @Valid TicketBackVo ticketBackVo) {
        return tktTicketService.backTicket(ticketBackVo);
    }

    /**
     * 获取关联工单
     */
    @GetMapping("/{orderNo}/tickets")
    public AjaxResult getTicketByOrderNo(@PathVariable("orderNo") String orderNo){
        return tktTicketService.getTicketByOrderNo(orderNo);
    }

    /**
     * 获取工单售后信息
     * */
    @GetMapping("/claims")
    public AjaxResult getClaims(Long ticketId){
        return tktTicketService.getClaims(ticketId);
    }
}
